package com.dony.shop.service.impl;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dony.common.utils.SecurityUtils;
import com.dony.common.utils.price.ExchangeRateUtils;
import com.dony.shop.domain.Item;
import com.dony.shop.domain.ItemModel;
import com.dony.shop.domain.enums.ShopeeCountryEnum;
import com.dony.shop.domain.vo.RpcPriceBody;
import com.dony.shop.service.IItemService;
import com.dony.shop.util.PriceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dony.shop.mapper.PriceConfigMapper;
import com.dony.shop.domain.PriceConfig;
import com.dony.shop.service.IPriceConfigService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 各站点价格配置Service业务层处理
 *
 * @author sundw
 * @date 2022-08-30
 */
@Service
public class PriceConfigServiceImpl extends ServiceImpl<PriceConfigMapper, PriceConfig> implements IPriceConfigService {

    @Resource
    IItemService itemService;
    @Resource
    IPriceConfigService priceConfigService;
    @Autowired
    ExchangeRateUtils exchangeRateUtils;

    @Override
    public PriceConfig selectPriceConfigByMerchantId(Long merchantId) {
        LambdaQueryWrapper<PriceConfig> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PriceConfig::getMerchantId, merchantId).eq(PriceConfig::getStatus, true);
        //防止有多个冲突了
        return this.list(wrapper).get(0);
    }

    @Override
    public RpcPriceBody getPriceByRpc(RpcPriceBody rpcPriceBody) {
        rpcPriceBody.setUrl2("https://vidream.cn");
        rpcPriceBody.setMobile("15000281997");
        rpcPriceBody.setSn2("kjzs");
        rpcPriceBody.setZjcb("0");
        //System.out.println(JSON.toJSONString(rpcPriceBody));
        String postResult = HttpRequest.post("https://vidream.cn/erp/v1/product/cj").timeout(-1).body(JSON.toJSONString(rpcPriceBody)).header("Referer", "https://vidream.cn/cangjia.html").execute().body();
        //String postResult = HttpUtil.post("https://vidream.cn/erp/v1/product/cj", JSON.toJSONString(rpcPriceBody));
        JSONObject reslut = JSONObject.parseObject(postResult);
        JSONObject data = (JSONObject) reslut.get("data");
        //如果查询不到数据
        if (data == null) return rpcPriceBody;
        rpcPriceBody.setBj(data.get("biaojia").toString());
        rpcPriceBody.setSj(data.get("sellPrice").toString());
        rpcPriceBody.setYf(data.get("yunfei").toString());
        rpcPriceBody.setLr(data.get("maoli").toString());
        rpcPriceBody.setSellPrice(data.getFloat("sellPrice"));
        return rpcPriceBody;
    }

    @Override
    public List<PriceConfig> listByParam(PriceConfig priceConfig) {
        QueryWrapper<PriceConfig> queryWrapper = new QueryWrapper<>();
        if (priceConfig != null && priceConfig.getMerchantId() != null) {
            queryWrapper.eq("merchant_id", priceConfig.getMerchantId());
        }
        queryWrapper.eq("tenant_id", SecurityUtils.getDeptId());
        return list(queryWrapper);
    }

    @Override
    public Integer getComputedPrice(ItemModel model) {
        Integer maolilv;
        Item item = itemService.getById(model.getItemId());
        String region = exchangeRateUtils.getRegionByCurrency(model.getCurrency()).toLowerCase();

        if (item != null && item.getMerchantId() != null) {
            PriceConfig priceConfig = priceConfigService.selectPriceConfigByMerchantId(item.getMerchantId());
            JSONObject pcJo = JSON.parseObject(JSON.toJSONString(priceConfig));
            maolilv =(Integer) pcJo.get(region);
        } else {
            maolilv = 30;
        }
        //当前站点code
        RpcPriceBody rpcPriceBody = new RpcPriceBody();
        rpcPriceBody.setPurchasePrice(Math.round(model.getInflatedPriceOfOriginalPrice()));
        rpcPriceBody.setMaolilv(maolilv.toString());
        rpcPriceBody.setSite(ShopeeCountryEnum.getRpcByName(region));
        //暂时用原价这个字段代替
        rpcPriceBody.setmWeight(model.getInflatedPriceOfCurrentPrice().toString());
        //折扣目前统一配置为20
        rpcPriceBody.setZhekou("20");
        rpcPriceBody.setFss("1");
        rpcPriceBody.setCcb("0");
        rpcPriceBody = priceConfigService.getPriceByRpc(rpcPriceBody);
        if (rpcPriceBody.getSellPrice() != null) {
            //对价格进行高位取整100
            Integer price = PriceUtil.fomatToInteger(rpcPriceBody.getSellPrice());
            return price;
        }
        return null;
    }
}
